﻿using Common;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace SFJDManagement
{
    public class IColumnVehicle : DataGridTemplateColumn
    {
        public override BindingBase ClipboardContentBinding { get => base.ClipboardContentBinding; set => base.ClipboardContentBinding = value; }
        public Binding Binding { set; get; }

        public override object OnCopyingCellClipboardContent(object item)
        {
            return base.OnCopyingCellClipboardContent(item);
        }

        public override void OnPastingCellClipboardContent(object item, object cellContent)
        {
            base.OnPastingCellClipboardContent(item, cellContent);
        }

        public override string ToString()
        {
            return base.ToString();
        }

        protected override void CancelCellEdit(FrameworkElement editingElement, object uneditedValue)
        {
            base.CancelCellEdit(editingElement, uneditedValue);
        }

        protected override bool CommitCellEdit(FrameworkElement editingElement)
        {
            return base.CommitCellEdit(editingElement);
        }

        protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
        {
            return base.GenerateEditingElement(cell, dataItem);
        }

        protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
        {
            Binding bind = Binding as Binding; //对grid进行数据绑定
            DataGrid grid = new DataGrid();
            grid.SetBinding(DataGrid.ItemsSourceProperty, bind);
            //grid.ItemsSource = (dataItem as ClassData).Vehicles;
            grid.CanUserAddRows = false;
            grid.Loaded += Grid_Loaded;

            //Binding bind = new Binding();
            //bind.Source = grid;
            //cell.SetBinding(DataGridCell.ContentProperty, bind);
            return grid;
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            DataGrid grid = sender as DataGrid;
            //RelativeSource rs = new RelativeSource(RelativeSourceMode.FindAncestor);//相对绑定，将header背景与row背景绑定
            //rs.AncestorType = typeof(DataGridCell);
            //Binding binding = new Binding("Background") { RelativeSource = rs };
            //grid.SetBinding(BackgroundProperty, new Binding("Background") { Path = binding.Path, Mode = BindingMode.TwoWay, RelativeSource = rs });
            foreach (DataGridColumn column in grid.Columns)
            {
                switch (column.Header)
                {
                    case "Number":
                        column.Header = "车号";
                        column.Width = 160;
                        break;
                    case "Brand":
                        column.Header = "品牌";
                        column.Width = 120;
                        break;
                    case "Type":
                        column.Header = "型号";
                        column.Width = 220;
                        break;
                    case "Procj":
                        column.Header = "车检";
                        column.Width = 34;
                        break;
                    case "Procs":
                        column.Header = "车速";
                        column.Width = 34;
                        break;
                    case "Prohj":
                        column.Header = "痕迹";
                        column.Width = 34;
                        break;
                    case "Pross":
                        column.Header = "损失";
                        column.Width = 34;
                        break;
                    case "Procy":
                        column.Header = "成因";
                        column.Width = 34;
                        break;
                    case "Progc":
                        column.Header = "过程";
                        column.Width = 34;
                        break;
                    case "Prosx":
                        column.Header = "属性";
                        column.Width = 34;
                        break;
                    case "Projsy":
                        column.Header = "驾驶员";
                        column.Width = 46;
                        break;
                    case "Profx":
                        column.Header = "方向";
                        column.Width = 34;
                        break;
                }
            }
        }

        protected override bool OnCoerceIsReadOnly(bool baseValue)
        {
            return base.OnCoerceIsReadOnly(baseValue);
        }

        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
        }

        protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs)
        {
            return base.PrepareCellForEdit(editingElement, editingEventArgs);
        }

        protected override void RefreshCellContent(FrameworkElement element, string propertyName)
        {
            base.RefreshCellContent(element, propertyName);
        }

        protected override bool ShouldSerializeProperty(DependencyProperty dp)
        {
            return base.ShouldSerializeProperty(dp);
        }
    }
}
